package com.sywb.chuangyebao.view.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.sywb.chuangyebao.contract.SearchResultContract;

import org.bining.footstone.log.Logger;

/**
 * 搜索结果
 */
public class SearchFragment extends BaseRecyclerFragment<SearchResultContract.Presenter> implements SearchResultContract.View {

    private String itemType;
    private String keyword;

    /**
     * Fragment 实例
     */
    public static SearchFragment newInstance(Object... pramars) {
        SearchFragment fragment = new SearchFragment();
        fragment.setArguments(getBundle(pramars));
        return fragment;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        super.initView(view, bundle);
        if (bundle == null) {
            itemType = getArguments().getString("p0", "all");
            keyword = getArguments().getString("p1", null);
        } else {
            itemType = bundle.getString("p0", "all");
            keyword = bundle.getString("p1", null);
        }
    }

    @Override
    public void initPresenter() {
        mPresenter.initPresenter(this);
        if (!TextUtils.isEmpty(keyword)) {
            mPresenter.initData(keyword, itemType);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putString("p0", itemType);
        outState.putString("p1", keyword);
        super.onSaveInstanceState(outState);
    }

    @Override
    public boolean useRxBus() {
        return false;
    }

    public void rxSearchDo(String data) {
        Logger.e("rxSearchDo:" + data);
        if (keyword == null || !data.equals(keyword)) {
            keyword = data;
            if (mPresenter != null) {
                mPresenter.initData(data, itemType);
            }
        }
    }
}
